{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true
   },
   "source": [
    "# 使用sklearn的逻辑回归接口实现MNIST分类"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import time\n",
    "import matplotlib.pyplot as plt\n",
    "import numpy as np\n",
    "from sklearn.datasets import fetch_openml\n",
    "from sklearn.linear_model import LogisticRegression\n",
    "from sklearn.model_selection import train_test_split\n",
    "from sklearn.preprocessing import StandardScaler\n",
    "from sklearn.utils import check_random_state"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "t0 = time.time()\n",
    "train_samples = 5000"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "X, y = fetch_openml('mnist_784', version=1, return_X_y=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "random_state = check_random_state(0)\n",
    "permutation = random_state.permutation(X.shape[0])\n",
    "X = X[permutation]\n",
    "y = y[permutation]\n",
    "x = X.reshape((X.shape[0], -1))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "x_train, x_test, y_train, y_test = train_test_split(X, y, train_size=train_samples, test_size=10000)\n",
    "\n",
    "scaler = StandardScaler()\n",
    "x_train = scaler.fit_transform(x_train)\n",
    "x_test = scaler.transform(x_test)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "clf = LogisticRegression(C=50. / train_samples, multi_class='multinomial', penalty='l1',\n",
    "                         solver='saga', tol=0.1)\n",
    "clf.fit(x_train, y_train)\n",
    "sparsity = np.mean(clf.coef_ == 0) * 100\n",
    "score = clf.score(x_test, y_test)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<Figure size 720x360 with 0 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "coef = clf.coef_.copy()\n",
    "plt.figure(figsize=(10, 5))\n",
    "scale = np.abs(coef).max()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAWAAAAD+CAYAAAAEet/LAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADh0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uMy4xLjEsIGh0dHA6Ly9tYXRwbG90bGliLm9yZy8QZhcZAAAgAElEQVR4nO2deZRcV33nP7+q3le1urVLrdbqTbbkDWNkG2wDAwHM5hAgbAnhHM6ByYkZkkwSyDiTw5jJMIScAGEO4IFA2AITQ8AJxrtwwCyWvMibLLktWXtL6la3eq+688fv3q7qolrdpa7qW9X9+5zT53W9evXeffe993vf+7u/+7vinMMwDMOYexKxC2AYhrFQMQNsGIYRCTPAhmEYkTADbBiGEQkzwIZhGJEwA2wYhhEJM8AVgIjcKiJfL+H+d4vIK/z/IiL/V0ROicgvRORaEXmmBMfsFJEBEUkWe9+Vgoi8WUQO+Hq4NHZ5jLnHDHCZICLvFJFf+YfxsIj8m4hcMxfHds5d5Jy733+8BngVsNo59xLn3A7n3HmzPYaIdIvIK7OOud851+ScS81233OBiDgR2Vjk3X4K+LCvh51F3rdRAZgBLgNE5CPAZ4D/ASwDOoHPA2+MUJy1QLdz7kyEY89LRKRqiq/WArvPcZ8LtuUwr3DO2V/EP6AVGAB++yzb3Ap8PevzPwNHgD7gQeCirO9+C3gS6AcOAh/16zuAHwK9wElgB5Dw33UDrwTeDwwDKV+mvwJeAbyYtf81wP8DjgMngM/69RuAe/26HuCfgEX+u68BaWDI7/dPgC7AAVV+m5XAD3zZngM+kHP+3wH+0Z/XbuCKKerqC8CnctZ9H/hI1n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wJ59yI//jFGZZhrohSJ8CLwPedc2POueeBZ1CDXA7EqpPA2ykv9wPEq5P3A9/x+/gZUAd0zOZEikwsm3LIOfcW59ylwF/4dX2zPhtKa4DvBWpF5ANhhYhcKSIvz9muFTjs37LvRpvIoWl4zDn3ReDLwGUi0gEknHPfAz4OXJZ7UBFZkfXxJuCpIp7TbIlSJ8AdqP8Mv/1mYF9Rz+zciVUniMh5QBvwsyKf02yJVSf7gRv9Pi5ADfDxop7Z7IhlUzpEJpJx/xlwe9HOyDlXsj9gJfpG3Yu+vX6EKq8u4Am/zSbgMeDnaO/igF//XuAJYCewA/XTbQUeAXb5v9fmOeZt/liPog7z80t5jhVSJwJ8GngSeBx4e+x6iF0n/re3Ap+Mff7lUidoZ9ND/tnZBbw6dj2USb3cDOxB+5S+BNQW63xsKLJhGEYkbCScYRhGJMwAG4ZhRMIMsGEYRiTMABuGYUTCDLBhGEYkzAAbhmFEwgywYRhGJMwAG4ZhROL/A53uJvVs3t+BAAAAAElFTkSuQmCC\n",
      "text/plain": [
       "<Figure size 432x288 with 10 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Sparsity with L1 penalty: 81.65%\n",
      "Test score with L1 penalty: 0.8344\n",
      "Example run in 46.391 s\n"
     ]
    }
   ],
   "source": [
    "for i in range(10):\n",
    "    l1_plot = plt.subplot(2, 5, i + 1)\n",
    "    l1_plot.imshow(coef[i].reshape(28, 28), interpolation='nearest',\n",
    "                   cmap=plt.cm.RdBu, vmin=-scale, vmax=scale)\n",
    "    l1_plot.set_xticks(())\n",
    "    l1_plot.set_yticks(())\n",
    "    l1_plot.set_xlabel('Class %i' % i)\n",
    "\n",
    "plt.suptitle('Classification vector for...')\n",
    "plt.show()\n",
    "print(\"Sparsity with L1 penalty: %.2f%%\" % sparsity)\n",
    "print(\"Test score with L1 penalty: %.4f\" % score)\n",
    "run_time = time.time() - t0\n",
    "print('Example run in %.3f s' % run_time)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.2"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": false,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
